package com.parktech.sun.domain.service.industryinfo.impl;

import com.parktech.sun.domain.model.industryinfo.IndustryInfo;
import com.parktech.sun.domain.model.industryinfo.IndustryInfoCategory;
import com.parktech.sun.domain.repository.Page;
import com.parktech.sun.domain.repository.industryinfo.IndustryInfoRepository;
import com.parktech.sun.domain.service.impl.BaseServiceImpl;
import com.parktech.sun.domain.service.industryinfo.IndustryInfoService;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * User: Think
 * Date: 11-1-13
 * Time: 下午9:05
 */
@Service
public class IndustryInfoServiceImpl extends BaseServiceImpl<IndustryInfo> implements IndustryInfoService {

    private IndustryInfoRepository industryInfoRepository;

    @Autowired
    public IndustryInfoServiceImpl(IndustryInfoRepository industryInfoRepository) {
        super(industryInfoRepository);
        this.industryInfoRepository = industryInfoRepository;
    }

    public Page searchCategory(String title, Page page) {

        Criteria criteria = industryInfoRepository.createCriteria(IndustryInfoCategory.class);

        if (StringUtils.isNotEmpty(title)) {
            criteria.add(Property.forName("title").like(title, MatchMode.ANYWHERE));
        }

        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("modifyTime"));
        return industryInfoRepository.page(criteria, page);
    }

    public IndustryInfoCategory getByCategoryId(Long categoryId) {
        return industryInfoRepository.getByCategoryId(categoryId);
    }

    public void saveOrUpdateCategory(IndustryInfoCategory industryInfoCategory) {
        industryInfoRepository.saveOrUpdateCategory(industryInfoCategory);
    }

    public Page search(String title, Long category, Integer publishStatus, Page page) {

        Criteria criteria = industryInfoRepository.createCriteria();

        if (StringUtils.isNotEmpty(title)) {
            criteria.add(Property.forName("title").like(title, MatchMode.ANYWHERE));
        }
        if (category != -1) {
            criteria.add(Property.forName("category.id").eq(category));
        }
        if (publishStatus != -1) {
            criteria.add(Property.forName("publishStatus").eq(publishStatus));
        }

        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.addOrder(Order.desc("modifyTime"));

        return industryInfoRepository.page(criteria, page);
    }

    public Page pageBy(Long categoryId, Page page) {

        Criteria criteria = industryInfoRepository.createCriteria();

        if (categoryId != -1) {
            criteria.add(Property.forName("category.id").eq(categoryId));
        }
        criteria.add(Property.forName("publishStatus").eq(1));
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.addOrder(Order.desc("modifyTime"));

        return industryInfoRepository.page(criteria, page);
    }

    public List<IndustryInfo> listTop6() {

        Criteria criteria = industryInfoRepository.createCriteria();

        criteria.add(Property.forName("publishStatus").eq(1));
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.addOrder(Order.desc("modifyTime"));
        criteria.setMaxResults(5);
        return criteria.list();
    }
}
